<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>


    var defaultHeight = "173cm";
    var defaultWeight = "65kg";


    // 声明对象
    var zhang = {
        name: "张三",

        // 对weight属性取值进行拦截
        get weight() {
            console.log("对weight取值", this);
            return defaultWeight;
        },
        // 对weight属性赋值进行拦截
        set weight(val) {
            console.log("对weight赋值", this);
            defaultWeight = val;
        }
    }

    // 自定义对象的属性和修饰
    Object.defineProperties(zhang, {
        age: {
            value: 18,
            writable: false,
            enumerable: true,
            configurable: false,
        },
        height: {
            enumerable: true,
            configurable: false,
            // 拦截器: 对该属性取值和赋值时触发 =
            get: function () {  //对该属性取值时触发  
                // return undefined; 取值时默认返回undefined

                console.log("对height取值", this);   // this-> 目标对象(zhang)
                return defaultHeight;
            },
            set: function (val) { // 对该属性赋值时触发   val => 形参用于接收对应的值
                console.log("对height赋值", this);
                defaultHeight = val;
            }
        }
    })

    // zhang.age = 28;
    // delete zhang.age;

    // console.log(zhang.height);

    // zhang.height = "188cm";
    // console.log(zhang.height);

    console.log(zhang.weight);

    zhang.weight = "70kg";

    console.log(zhang.weight);

    console.log(zhang);


</script>

</html>